#include "ogre_gui_test.h"

#include "space\space.h"

#include <OGRE\OgreRoot.h>
#include <OGRE\OgreConfig.h>
#include <OGRE\OgreConfigFile.h>
#include <OGRE\OgreRenderSystem.h>
#include <OGRE\OgreSceneManager.h>
#include <OGRE\OgreCamera.h>
#include <OGRE\OgreViewport.h>
#include <OGRE\OgreRenderWindow.h>
#include <OGRE\OgreEntity.h>
#include <OGRE\OgreSceneNode.h>
#include <OGRE\OgreLight.h>

void ogre_standalone_test(void) {
	Ogre::Root				* oRoot;
	Ogre::RenderWindow		* oRenderWindow;
	Ogre::Camera			* oCamera;
	Ogre::Viewport			* oViewport;
	Ogre::SceneManager		* oSceneManager;
	Ogre::Entity			* oEntity;
	Ogre::SceneNode			* oSceneNode;

	try {

		oRoot = new Ogre::Root("plugins_d.cfg");

		Ogre::ConfigFile cf;
		cf.load("resources_d.cfg");
		Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();

		Ogre::String secName,
			typeName,
			archName;

		LOG("Loading configured resources.")
		while(seci.hasMoreElements())
		{
			secName = seci.peekNextKey();
			Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
			Ogre::ConfigFile::SettingsMultiMap::iterator i;
			for (i = settings->begin(); i != settings->end(); ++i)
			{
				typeName = i->first;
				archName = i->second;
				Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
					archName, typeName, secName);
			}
			LOG("\t%s", secName.c_str());
		}
	
		LOG("Checking render systems");
		Ogre::RenderSystemList rList = oRoot->getAvailableRenderers();
		Ogre::RenderSystemList::iterator i = rList.begin();
		Ogre::RenderSystem* RenderSys, *SetRenderSys;
		while (i != rList.end()) {
			RenderSys = (*i++);
			LOG("Found render system: [%s]", RenderSys->getName().c_str());
			if(RenderSys->getName().find("Direct3D9 Rendering Subsystem") != std::string::npos) {
			//if(RenderSys->getName().find("OpenGL Rendering Subsystem") != std::string::npos) {
				SetRenderSys = RenderSys;
			}
		}

		LOG("Attempting initialization");
		LOG("\tSetting render system: [%s]", SetRenderSys->getName().c_str());

		//SetRenderSys->setConfigOption( "FSAA", "8" );
		//SetRenderSys->setConfigOption( "Video Mode", "1024 x 768" );
		//SetRenderSys->setConfigOption( "Colour Depth", "32");

		oRoot->setRenderSystem(SetRenderSys);

		oRoot->showConfigDialog();
		oRoot->saveConfig();

		oRoot->initialise(false);
	
		oRenderWindow = oRoot->createRenderWindow("TEST TEST TEST", 1024, 768, false);

		LOG("\tCreating scene");
		oSceneManager = oRoot->createSceneManager(Ogre::ST_GENERIC, "Main");
		oSceneManager->setAmbientLight(Ogre::ColourValue(0.5f, 0.5f, 0.5f));

		oCamera = oSceneManager->createCamera("Main Camera");
		oCamera->setPosition(0,0,500);
		oCamera->lookAt(100, 0, 0);

		oViewport = oRenderWindow->addViewport(oCamera);

		oViewport->setBackgroundColour(Ogre::ColourValue::Blue);

		//oEntity = oSceneManager->createEntity("Ninja", "ninja.mesh");
		//oSceneNode = oSceneManager->getRootSceneNode()->createChildSceneNode("NinjaNode", Ogre::Vector3( 100, 0, 0 ) );
		//oSceneNode->attachObject(oEntity);

		

		Ogre::Light* light = oSceneManager->createLight("MainLight");
		light->setPosition(20.0f, 80.0f, 50.0f);

		oRenderWindow->setActive(true);
		oRenderWindow->update();

		//oRoot->startRendering();
	
		LOG("OGRE Standalone test complete.");
		LOG("Press any key to continue.");
		getchar();

		delete oRoot;
	} catch(Ogre::Exception & e) {
		LOG("EXCEPTION! [%s]", e.getFullDescription().c_str());

	}
}